<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文件标签" prop="tag">
        <el-select v-model="queryParams.tag" clearable placeholder="请选择">
          <el-option
            v-for="item in pageTagOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:fileUploadRecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:fileUploadRecord:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:fileUploadRecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:fileUploadRecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fileUploadRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center"  type="index"   v-if="columns[0].visible"/>
      <el-table-column label="文件名" align="center" prop="name"   v-if="columns[1].visible" />
      <el-table-column label="类目" align="center" prop="fileType"   v-if="columns[6].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.file_type" :value="scope.row.fileType"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="标签" align="center" prop="tag"    v-if="columns[2].visible"/>-->

      <el-table-column label="描述" align="center" prop="fileDetail"   v-if="columns[4].visible"/>
      <el-table-column label="创建人" align="center" prop="userName"   v-if="columns[3].visible"/>
      <el-table-column label="下载" align="center" prop="visitUrl"   v-if="columns[5].visible">
        <template slot-scope="scope">
          <a STYLE="color: #00afff" target="_blank" :href="scope.row.visitUrl"  v-hasPermi="['module:fileUploadRecord:download']">下载</a>
        </template>
      </el-table-column>

      <el-table-column label="上传时间" align="center" prop="createTime"   v-if="columns[11].visible"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  >

        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:fileUploadRecord:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:fileUploadRecord:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文件上传记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文件标签" prop="tag">
          <el-select v-model="form.tag" clearable placeholder="请选择">
            <el-option
              v-for="item in pageTagOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类目" prop="fileType">
          <el-select v-model="form.fileType" clearable placeholder="请选择">
            <el-option
              v-for="item in pageFileTypeOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="描述" prop="fileDetail">
          <el-input v-model="form.fileDetail" type="textarea" placeholder="请输入内容" />
        </el-form-item>
         <el-upload
          class="upload-demo"
          ref="upload"
          :action="uploadFileUrl"
          :on-success="handlsuccess"
          :headers="headers"
          :file-list="fileList"
          :multiple="false"
          :auto-upload="false">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>

        </el-upload>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFileUploadRecord, getFileUploadRecord, delFileUploadRecord, addFileUploadRecord, updateFileUploadRecord,getDownloadApi } from "@/api/module/fileUploadRecord";
import {getToken} from "@/utils/auth";
import {getDicts} from "@/api/system/dict/data";

export default {
  name: "FileUploadRecord",
  dicts:['file_type'],
  data() {
    return {
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/module/fileUploadRecord/uploadFile", // 上传文件服务器地址
      fileList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件上传记录表格数据
      fileUploadRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        suffix: null,
        localUrl: null,
        visitUrl: null,
        size: null,
        userId: null,
        userName: null,
        fileType: null,
        fileDetail: null,
        state: null,
        tag:"my",
        createTime:null
      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
      },
      columns: [
        { key: 0, label: `序号`, visible: true },
        { key: 1, label: `文件名`, visible: true },
        { key: 2, label: `标签`, visible: true },
        { key: 3, label: `创建人`, visible: true },
        { key: 4, label: `描述`, visible: true },
        { key: 5, label: `下载`, visible: true },
        { key: 6, label: `文件类型`, visible: true },
        { key: 7, label: `后缀名`, visible: true },
        { key: 8, label: `本地路径`, visible: true },
        { key: 9, label: `文件大小`, visible: true },
        { key: 10, label: `状态`, visible: true },
        { key: 11, label: `上传时间`, visible: true },

      ],
     pageTagOptions:[],//文档标签
      pageFileTypeOptions:[],//文件类目
      updateShow:false,//是否上传成功

    };

  },
  created() {
   this.getpagetagOption()
    this.getpageFileTypeOption()
    this.getList();
  },
  methods: {
    async getpagetagOption(){
      let res=await getDicts('page_type')
      this.pageTagOptions=res.data
    },
    async getpageFileTypeOption(){
      let res=await getDicts('file_type')
      this.pageFileTypeOptions=res.data
    },
    //上传成功
    handlsuccess(response, file, fileList){
      this.form.filePath=response.data.filePath
      this.form.suffix=response.data.suffix
      this.form.name=response.data.name
      this.form.serverFileName=response.data.serverFileName
      this.form.size=response.data.size
      this.form.visitUrl=response.data.visitUrl
     this.updateShow=true
    },
    submitUpload() {
      this.$refs.upload.submit()
    },



    /** 查询文件上传记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.userId=this.$store.getters.id;
      listFileUploadRecord(this.queryParams).then(response => {
        this.fileUploadRecordList = response.rows;
        let arr=this.fileUploadRecordList
        arr.forEach(item=>{
          item.tag=this.getValue(this.pageTagOptions,item.tag)
        })
        this.fileUploadRecordList=arr
        this.total = response.total;
        this.loading = false;
      });
    },
    //回显处理
    getValue(arr, item) {
      for (let i = 0; i < arr.length; i++) {
        if (item === arr[i].dictValue) {
          return arr[i].dictLabel
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        suffix: null,
        localUrl: null,
        visitUrl: null,
        size: null,
        userId: null,
        userName: null,
        fileType: null,
        fileDetail: null,
        state: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.tag="my";
      this.open = true;
      this.title = "添加文件上传记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFileUploadRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文件上传记录";
      });
    },
    /** 提交按钮 */
    async submitForm() {

      this.$refs["form"].validate(valid => {
        if (valid) {

if(this.updateShow){
            if (this.form.id != null) {
              updateFileUploadRecord(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addFileUploadRecord(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
}else {
  this.$message.error("上传失败")
}
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除文件上传记录编号为"' + ids + '"的数据项？').then(function() {
        return delFileUploadRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/fileUploadRecord/export', {
        ...this.queryParams
      }, `fileUploadRecord_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
